package com.microservices.yucloud.appweb.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.microservices.yucloud.appweb.domain.UserInfo;
import com.microservices.yucloud.appweb.enums.UserSexEnum;
import com.microservices.yucloud.appweb.service.IUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-09-24
 */
@Slf4j
@RestController
@RequestMapping("/user-info")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @GetMapping("/add")
    public Boolean add() {
        UserInfo userInfo = new UserInfo();
        userInfo.setName("mark");
        userInfo.setAge(19);
        userInfo.setEmail("mark@mail.com");
        userInfo.setDeleted(0);
        userInfo.setSex(UserSexEnum.MAN);
        userInfo.setCreateTime(LocalDateTime.now());
        return userInfoService.save(userInfo);
    }

    @GetMapping("/update")
    public Boolean update() {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setSex(UserSexEnum.MAN);
        userInfo.setCreateTime(LocalDateTime.now());
        return userInfoService.updateById(userInfo);
    }

    @GetMapping("/list")
    public Object list() {
        log.info("查询用户列表开始");
        return userInfoService.list();
    }

    @GetMapping("/page")
    public Object page() {
        log.info("查询用户列表分页开始");
        IPage<UserInfo> page = new Page<>();
        page.setSize(20);
        return userInfoService.page(page);
    }

    @GetMapping("/{id}")
    public Object getById(@PathVariable int id) {
        log.info("查询用户信息【{}】", id);
        return userInfoService.getById(id);
    }

    @GetMapping("/delete/{id}")
    public Boolean testGetById(@PathVariable int id) {
        log.info("逻辑删除用户信息【{}】", id);
        return userInfoService.removeById(id);
    }

}
